/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-07-28
 * Time: 20:06
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }


    public TreeNode root;

    //搜索二叉树的中序遍历，便于打印出来
    public void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);

    }
    /**
     * 查找key是否存在于二叉搜索树当中
     * @param key
     * @return 当前节点找到返回地址  找不到返回null
     */
    public TreeNode search(int key) {
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > key){
                cur = cur.left;
            }else if(cur.val < key){
                cur = cur.left;
            }else{
                return cur;
            }
        }
        return null;
    }
    /**
     * 插入节点
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);//要插入的节点
        TreeNode cur = root;
        TreeNode parent = null;//定义cur的父亲节点
        if (root == null) {
            root = node;
            return true;
        }
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                //存在相同的元素 不能插入成功
                return false;
            }
        }
        //cur为null，parent执行cur的上一个
        if (parent.val > key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }
    /***
     * 删除关键字为key的节点
     * @param key
     */
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val > key){
                parent = cur;
                cur = cur.left;
            }else if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else{
                removeNode(cur,parent);
                return;
            }
        }
    }
    private void removeNode(TreeNode cur, TreeNode parent) {
        if(cur.left == null){
            if(cur == root){
                root = root.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else if (cur == parent.right){
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = root.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else if (cur == parent.right){
                parent.right = cur.left;
            }
        }else {
            TreeNode parentTarget = cur;
            TreeNode target = cur.right;
            while(target.left != null){
                parentTarget = target;
                target = target.left;
            }
            cur.val = target.val;
            //分两种情况
            if(parentTarget.left == target){
                parentTarget.left = target.right;
            }else if(parentTarget.right == target){
                parentTarget.right = target.right;
            }
        }
    }

}